#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <errno.h>
#include "../glob.h"

#define SERVER_PORT 			9527
#define IP_ADDRESS 				"192.168.2.71"

#define DEFAULT_BRIDGE_DEV 		"eth0"


static int  _msg_get_ip()
{
	int inet_sock;
	int ip_addr = 0;
	struct ifreq ifr;
	inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
	
	memset(&ifr,0,sizeof(struct ifreq));

	strcpy(ifr.ifr_name, DEFAULT_BRIDGE_DEV);
	//SIOCGIFADDR标志代表获取接口地址
	if (ioctl(inet_sock, SIOCGIFADDR, &ifr) <  0)
			perror("ioctl");

	ip_addr = (int)((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr.s_addr;
	
	DEBUG("%s\n", inet_ntoa(((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr));
	
	

	close(inet_sock);

	return ip_addr;
}

static int _msg_get_netmask()
{
	int inet_sock;
	struct ifreq ifr;
	int netmask;
	inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
	
	memset(&ifr,0,sizeof(struct ifreq));

	strcpy(ifr.ifr_name, DEFAULT_BRIDGE_DEV);
	//SIOCGIFADDR标志代表获取接口地址
	if (ioctl(inet_sock, SIOCGIFNETMASK, &ifr) <  0)
			perror("ioctl");
	
	netmask = (int)((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr.s_addr;

	close(inet_sock);

	return netmask;
}

static int _msg_get_broadcast_ip()
{
	int ip_addr;
	int netmask;
	int net;
	
	ip_addr = _msg_get_ip();
	netmask = _msg_get_netmask();
	
	//190.168.1.5 
	//255.255.255.128
	
	net = ntohl(ip_addr) & ntohl(netmask);
	
	DEBUG("ip_addr=%08x",ip_addr);
	DEBUG("netmask=%08x",netmask);	
	DEBUG("net=%08x",net);	
	
	DEBUG("192.168.1.10 %08x",inet_addr("192.168.1.10"));
	
	net = ntohl(inet_addr("192.168.1.5")) & ntohl(inet_addr("255.255.255.128"));
	DEBUG("192.168.1.5 %08x",ntohl(inet_addr("192.168.1.5")));
	DEBUG("255.255.255.128 %08x",ntohl(inet_addr("255.255.255.128")));
	
	DEBUG("mask ip=%08x",ntohl(inet_addr("255.255.255.255"))-ntohl(inet_addr("255.255.255.128")));
	
	
	DEBUG("net=%08x",net);	
	DEBUG("boadcast %08x",ntohl(inet_addr("255.255.255.255"))-ntohl(inet_addr("255.255.255.128"))+net);
	
	DEBUG("INADDR_BROADCAST=%08x",INADDR_BROADCAST);
	

	return (net-1);
}



int broadcast()
{
	int 		sock;
	int			opt = 1;
	char 		szBuff[123] = "cbbxxxxxxxxxxxxxxxx";
	struct sockaddr_in ser_addr;
	
	
	sock=socket(AF_INET, SOCK_DGRAM, 0);
	
	setsockopt(sock,SOL_SOCKET, SO_BROADCAST,&opt,sizeof(opt));
	
	ser_addr.sin_family = AF_INET;
    ser_addr.sin_port = htons(SERVER_PORT);
    ser_addr.sin_addr.s_addr = inet_addr("255.255.255.255");
	
	sendto(sock, szBuff, strlen(szBuff), 0, (struct sockaddr*)&ser_addr, sizeof(ser_addr));
	
	close(sock);
	
	
	return 0;
}

int udp_cli()
{
	int 		sock;
	int 		status;
	int 		len;
	char		szPkt[128] = {0};
	char 		szBuff[123] = "cbbxxxxxxxxxxxxxxxx";
	char 		recv_buff[123] = "";
	struct sockaddr_in ser_addr;
	struct sockaddr_in cli_addr;

	
	sock=socket(AF_INET, SOCK_DGRAM, 0);
	ser_addr.sin_family = AF_INET;
    ser_addr.sin_port = htons(SERVER_PORT);
    ser_addr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
	
	while(1)
	{	
		sleep(2);
		status = sendto(sock, szBuff, strlen(szBuff), 0, (struct sockaddr*)&ser_addr, sizeof(ser_addr));
		printf("status = %d\n",status);
		recvfrom(sock,recv_buff,sizeof(recv_buff),0,(struct sockaddr *)&ser_addr,(socklen_t *)(&len));
		printf("recv buff is %s\n",recv_buff);
		
		//printf("send result is %s\n",strerror(errno));
	}

	return 0;
}

int main(int argc, char *argv[])
{
	_msg_get_broadcast_ip();	
	//udp_cli();
	
	
	return 0;
}